package elitech.vietnam.myfashion.view;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;

public class MyShoppingView extends LinearLayout implements OnClickListener {

	private MainActivity mContext;
	private LinearLayout mBtnFavor, mBtnBuying, mLayList;
	public FavoritesListView mFavorView;
	public BuyingListView mBuyingView;
	private LinearLayout mMainMenu;

	public MyShoppingView(MainActivity context) {
		super(context);
		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.myshopping, this);
		}
		
		this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		
		mBtnFavor = (LinearLayout) findViewById(R.id.myshoping_layBtnFavor);
		mBtnBuying = (LinearLayout) findViewById(R.id.myshoping_layBtnBuying);
		mLayList = (LinearLayout) findViewById(R.id.myshoping_layList);
		mMainMenu = (LinearLayout) findViewById(R.id.main_menu);
		mMainMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mContext.callMenu();
			}
		});
		
		mBtnFavor.setOnClickListener(this);
		mBtnBuying.setOnClickListener(this);
		
		mBtnFavor.setBackgroundColor(Color.TRANSPARENT);
		mBtnBuying.setBackgroundColor(getResources().getColor(R.color.dark_orange));
		
		mFavorView = new FavoritesListView(mContext);
		mFavorView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		mBuyingView = new BuyingListView(mContext);
		mLayList.addView(mFavorView);
		if (mContext.mUser == null)
			GetMember();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.myshoping_layBtnFavor:
			showFavor();
			break;
		case R.id.myshoping_layBtnBuying:
			showBuying();
			break;
		}

	}
	
	public void showBuying(){
		mBtnBuying.setBackgroundColor(Color.TRANSPARENT);
		mBtnFavor.setBackgroundColor(getResources().getColor(R.color.dark_orange));
		mLayList.removeAllViews();
		mLayList.addView(mBuyingView);
	}
	
	public void showFavor(){
		mBtnFavor.setBackgroundColor(Color.TRANSPARENT);
		mBtnBuying.setBackgroundColor(getResources().getColor(R.color.dark_orange));
		mLayList.removeAllViews();
		mLayList.addView(mFavorView);
	}
	
	private void GetMember() {
		new CallServiceTask() {
			public void work() {
				try {
					mContext.mUser = service.getInfoMemberByID(mContext.mUser.Id, -1);
				} catch (IOException e) {
				}
				catch (XmlPullParserException e) {}
			};

			protected void onPostExecute(Void result) {
			};
		}.execute();
	}
	
	public void onResume() {
		mFavorView.onResume();
	}
	
}
